import * as types from "../mutation-types";
import { constantRouterMap } from "../../router";
import {
    getToken,
    setToken,
    removeToken,
    getAdminId,
    setAdminId,
    removeAdminId,
    getLanguage,
    setLanguage,
    removeLanguage,
    getRouters,
    setRouters,
    removeRouters
} from "../../utils/auth";
// import { $NOT_NETWORK } from '../../utils/errorCode'
import { Message } from "element-ui";

// initial state
const state = {
    adminId: getAdminId(), // id
    userName: "", // 昵称
    language: getLanguage(),
    avatar: "", // 头像
    token: getToken(), // 登录token
    authRules: [], // 权限列表
    routers: "" // 路由列表
};

// getters
const getters = {
    adminId: state => state.adminId,
    userName: state => state.userName,
    avatar: state => state.avatar,
    token: state => state.token,
    authRules: state => state.authRules,
    routers: state => state.routers,
    language: state => state.language
};

// actions
const actions = {
    //设置角色id
    setAdminId({ commit }, data){
        commit(types.RECEIVE_ADMIN_ID, data)
    },
    // 用户名登录
    loginName({ commit }, res) {

        // commit(types.RECEIVE_ADMIN_ID, res.user.code);
        // commit(types.RECEIVE_ADMIN_LANGUAGE, res.user.language);
        commit(types.RECEIVE_ADMIN_TOKEN, res.access_token);
        // commit(types.RECEIVE_ADMIN_NAME, res.user.username);
        // commit(types.RECEIVE_ADMIN_AUTH_RULES, []);

    },
    userInfo({ commit }, res) {
        return new Promise((res, rej) => {
            // const data = response.data || {};
            // commit(types.RECEIVE_ADMIN_NAME, res.user.username);
            // commit(types.RECEIVE_ADMIN_AVATAR, res.user.logo);
            // commit(types.RECEIVE_ADMIN_AUTH_RULES, [res.user.role.code]);
            res("dfsfsd")
        })
    },
    // 登出
    loginOut({ commit }) {
        return new Promise((res, rej) => {
            // commit(types.RECEIVE_ADMIN_ID, "");
            // commit(types.RECEIVE_ADMIN_LANGUAGE, "");
            commit(types.RECEIVE_ADMIN_TOKEN, "");
            // commit(types.RECEIVE_ADMIN_AUTH_RULES, []);
            res()
        }).catch()
        

    },

    // 前端 登出
    fedLogout({ commit }) {
        commit(types.RECEIVE_ADMIN_ID, "");
        commit(types.RECEIVE_ADMIN_TOKEN, "");
        commit(types.RECEIVE_ADMIN_AUTH_RULES, []);
        commit(types.RECEIVE_ROUTERS, []);

    },
    /**
     * 过滤路由列表
     * @param commit
     * @param data
     * @returns {Promise}
     */
    filterRouter({ commit }, data) {
        const { accessedRouters } = data;
        if (accessedRouters && accessedRouters.length > 0) {
            commit(types.RECEIVE_ROUTERS, accessedRouters);
        }
    }
};

// mutations
const mutations = {
    [types.RECEIVE_ADMIN_ID](state, adminId) {
        state.adminId = adminId;
        if (adminId === "") {
            removeAdminId();
        } else {
            setAdminId(adminId);
        }
    },
    [types.RECEIVE_ADMIN_TOKEN](state, token) {
        state.token = token;
        if (token === "") {
            removeToken();
        } else {
            setToken(token);
        }
    },
    [types.RECEIVE_ADMIN_LANGUAGE](state, language) {
        state.language = language;
        if (language === "") {
            removeLanguage();
        } else {
            setLanguage(language);
        }
    },
    [types.RECEIVE_ADMIN_NAME](state, userName) {
        state.userName = userName;
    },
    [types.RECEIVE_ADMIN_AVATAR](state, avatar) {
        state.avatar = avatar;
    },
    [types.RECEIVE_ADMIN_AUTH_RULES](state, authRules) {
        state.authRules = authRules;
    },
    [types.RECEIVE_ROUTERS](state, routers) {
        const tempRm = constantRouterMap.concat(routers);
        let tempRmJson = JSON.parse(JSON.stringify(tempRm))
        
        state.routers = tempRmJson;
        // if (routers === []) {
        //     removeRouters();
        // } else {
        //     setRouters(strrouters);
        // }
    }
};

export default {
    state,
    getters,
    actions,
    mutations
};
